﻿Imports System.ComponentModel

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        isLoad = False
        isCal = False
        isDraw = False
        isLoadPic = False
    End Sub

    Private Sub Txt文件ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Txt文件ToolStripMenuItem.Click
        loadData()
        If isLoad = False Then
            Exit Sub
        End If
        ZDataGridView1.DataSource = table()
    End Sub


    Private Sub 图片ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 图片ToolStripMenuItem.Click, 添加图片ToolStripMenuItem.Click
        input()
        'If PictureBox1.Image Is Nothing Then
        '    Exit Sub
        'End If
        PictureBox1.Image = Picture1
        If TabControl1.SelectedIndex <> 1 Then
            TabControl1.SelectedIndex = 1
        End If
    End Sub

    Private Sub MoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MoToolStripMenuItem.Click， Moravec算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        Moravec(Picture1)
        PictureBox2.Image = resultImg2
        isCal = True
    End Sub

    Private Sub 图片ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 图片ToolStripMenuItem1.Click, 保存图片ToolStripMenuItem.Click
        If TabControl1.SelectedIndex = 0 Then
            If Not isLoad Then
                MsgBox("请先导入数据！！", vbOKOnly, "提示")
                Exit Sub
            End If
            If Not isCal Then
                MsgBox("请先计算！！", vbOKOnly, "提示")
                Exit Sub
            End If
            If Not isDraw Then
                MsgBox("请先计算！！", vbOKOnly, "提示")
                Exit Sub
            End If
            savePicture(ZChart1)
        ElseIf TabControl1.SelectedIndex = 1
            If PictureBox2.Image IsNot Nothing Then
                SaveJPG(PictureBox2.Image)
            Else
                MsgBox("未进行任何特征提取操作！！", vbOKOnly, "提示")
                Exit Sub
            End If
        End If
    End Sub

    Private Sub 道ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 道ToolStripMenuItem.Click
        If Not isLoad Then
            MsgBox("请先导入数据！！", vbOKOnly, "提示")
            Exit Sub
        End If
        cal()
        ZTextBox2.Font = New Font("微软雅黑", 12)
        ZTextBox2.Text = ZMainModule.report()

        If Not isCal Then
            MsgBox("请重新计算！！", vbOKOnly, "提示")
            Exit Sub
        End If

        ZMainModule.draw(ZChart1)
        If Not isDraw Then
            MsgBox("请重新计算！！", vbOKOnly, "提示")
            Exit Sub
        End If
        TabControl2.SelectedIndex = 1
    End Sub

    Private Sub 一阶梯度差分算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 一阶梯度差分算子ToolStripMenuItem.Click, 梯度算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        FirOrderDifferGrad(Picture1)
        PictureBox2.Image = resultImg2
    End Sub

    Private Sub 一阶方向差分算子以北方向为例ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 一阶方向差分算子以北方向为例ToolStripMenuItem1.Click, 一阶方向差分算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        FirDiff(PictureBox1, PictureBox2)
    End Sub

    Private Sub 二阶方向差分算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 二阶方向差分算子ToolStripMenuItem.Click, 二阶方向差分算子ToolStripMenuItem1.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        SecDiff(PictureBox1, PictureBox2)
    End Sub

    Private Sub 拉普拉斯算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 拉普拉斯算子ToolStripMenuItem.Click, 拉普拉斯算子ToolStripMenuItem1.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        Laplace(PictureBox1, PictureBox2)
    End Sub

    Private Sub 一阶方向差分算子以北方向为例ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 一阶方向差分算子以北方向为例ToolStripMenuItem.Click, Roberts算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        '改为一阶差分Robert's梯度算子
        Roberts(Picture1)
        PictureBox2.Image = resultImg2
    End Sub

    Private Sub 一阶差分Prewitt算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 一阶差分Prewitt算子ToolStripMenuItem.Click, Prewitt算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        Prew(PictureBox1, PictureBox2)
    End Sub

    Private Sub 一阶差分Sober算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 一阶差分Sober算子ToolStripMenuItem.Click, Sobel算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        Sobel(PictureBox1, PictureBox2)
    End Sub

    Private Sub LOG算子ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LOG算子ToolStripMenuItem.Click, LOG算子ToolStripMenuItem1.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        Log(PictureBox1, PictureBox2)
    End Sub

    Private Sub ForstnerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ForstnerToolStripMenuItem.Click， Forstner算子ToolStripMenuItem.Click
        If Not isLoadPic Then
            MsgBox("请先导入图片！！", vbOKOnly, "提示")
            Exit Sub
        End If
        f(PictureBox1, PictureBox2)
    End Sub

    Private Sub 报告ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 报告ToolStripMenuItem.Click
        If Not isLoad Then
            MsgBox("请先导入数据！！", vbOKOnly, "提示")
            Exit Sub
        End If
        If Not isCal Then
            MsgBox("请先计算！！", vbOKOnly, "提示")
            Exit Sub
        End If
        saveReport(ZTextBox2)
    End Sub

    Private Sub 清除ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 清除ToolStripMenuItem.Click
        PictureBox2.Image = Nothing
    End Sub
    Dim check As Boolean
    Dim mouseP As Point
    Private Sub zchart1_MouseUp(sender As Object, e As MouseEventArgs) Handles ZChart1.MouseUp
        check = False
    End Sub

    Private Sub zchart1_MouseDown(sender As Object, e As MouseEventArgs) Handles ZChart1.MouseDown
        mouseP = e.Location
        check = True
    End Sub

    Private Sub zchart1_MouseMove(sender As Object, e As MouseEventArgs) Handles ZChart1.MouseMove
        If check Then
            System.Windows.Forms.Cursor.Current = Cursors.Hand
            Dim dx = e.Location.X - mouseP.X
            Dim dy = e.Location.Y - mouseP.Y
            ZChart1.Location = New Point(ZChart1.Location.X + dx, ZChart1.Location.Y + dy)
        End If
    End Sub

    Private Sub 放大ToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Dim oldHeight = ZChart1.Height
        Dim oldWidth = ZChart1.Width
        ZChart1.Height *= 1.1
        ZChart1.Width *= 1.1
        Dim p As Point = mouseP
        Dim l As Point = ZChart1.Location
        l.X -= 0.1 * p.X : l.Y -= 0.1 * p.Y
        ZChart1.Location = l
    End Sub

    Private Sub 缩小ToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Dim oldHeight = ZChart1.Height
        Dim oldWidth = ZChart1.Width
        ZChart1.Height *= 0.9
        ZChart1.Width *= 0.9
        Dim p As Point = mouseP
        Dim l As Point = ZChart1.Location
        l.X += 0.1 * p.X : l.Y += 0.1 * p.Y
        ZChart1.Location = l
    End Sub

    Private Sub zchart1_MouseWheel(sender As Object, e As MouseEventArgs) Handles ZChart1.MouseWheel
        mouseP = e.Location
        If e.Delta > 0 Then
            放大ToolStripMenuItem_Click(sender, e)
        Else
            缩小ToolStripMenuItem_Click(sender, e)
        End If
    End Sub

    Private Sub 坐标转换ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 坐标转换ToolStripMenuItem.Click
        DCoordinateTransform.Show()
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 退出ToolStripMenuItem.Click
        If MsgBox("是否退出？", vbOKCancel, "提示") = MsgBoxResult.Ok Then
            Application.Exit()
        End If
    End Sub

    Private Sub 刷新ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 刷新ToolStripMenuItem.Click
        If MsgBox("是否刷新？", vbOKCancel, "提示") = MsgBoxResult.Ok Then
            Application.Restart()
        End If
    End Sub

    Private Sub SToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SToolStripMenuItem.Click
        HelpWindow.Show()
    End Sub
End Class
